package com.example.android_demo2.workmanager

import android.content.Context
import androidx.work.*

/**
 * 定义工作
 */
class UploadWorker(appContext: Context, workerParams: WorkerParameters): Worker(appContext, workerParams) {
    override fun doWork(): Result {
//        uploadImage()
        val imageUriInput = inputData.getString("IMAGE_URL") ?: return Result.failure()
        return Result.success()
    }

    /**
     * 定义工作后，必须使用 WorkManager 服务进行调度该工作才能运行。对于如何调度工作，WorkManager 提供了很大的灵活性。您可以将其安排为在某段时间内定期运行，也可以将其安排为仅运行一次。
     */
    private val uploadWorkRequest: WorkRequest = OneTimeWorkRequestBuilder<UploadWorker>()
        .build()

    fun test() {
        WorkManager
            .getInstance(applicationContext)
            .enqueue(uploadWorkRequest)
    }
}



/**
*将 WorkRequest 提交给系统
最后，您需要使用 enqueue() 方法将 WorkRequest 提交到 WorkManager。
* */


fun test(context: Context) {


    val myWorkManagerRequest = OneTimeWorkRequestBuilder<UploadWorker>()
        .addTag("cleanUp")
        .build()

//    val sendLogsWorkRequest = PeriodicWorkRequestBuilder<SendLogsWorker>(24, TimeUnit.HOURS)
//        .setConstraints(Constraints.Builder()
//            .setRequiresCharging(true)
//            .build()
//        )
//        .build()
//    WorkManager.getInstance(context)
//        .enqueueUniquePeriodicWork(
//            "sendLog",
//            ExistingPeriodicWorkPolicy.KEEP,
//            sendLogsWorkRequest
//
//        )

}

//WorkManager
//.getInstance(myContext)
//.enqueue(uploadWorkRequest)

